package com.platform.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.modules.sys.entity.SysCacheEntity;
import com.platform.modules.sys.service.SysCacheService;

/**
 * 系统缓存管理
 *
 * @author 肖亮
 */
@Api(tags = "SysCacheController|系统缓存管理")
@RestController
@RequestMapping("/sys/cache")
public class SysCacheController {
	@Autowired
	SysCacheService sysCacheService;

	/**
	 * 查询缓存
	 *
	 * @param params
	 *            查询参数
	 * @return
	 */
	
	@ApiOperation(value = "查询缓存列表", notes = "根据条件获取所有的缓存")
	  @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "type", value = "缓存类型", dataType = "String")
    })
	@RequiresPermissions("sys:cache:queryAll")
	@PostMapping("/queryAll")
	public RestResponse queryAll(String type) {
		
		 if(type==null||type.equals("")) {
			 type="1";
		 }
		
		
	    Map<String,Object> params=new HashMap<String,Object>();
		if (Constant.STR_ONE.equals(type)) {
			// 查询所有缓存
			params.put("pattern", "*");
		} else if (Constant.STR_TWO.equals(type)) {
			// 查询session缓存
			params.put("pattern", Constant.SESSION + "*");
		} else if (Constant.STR_THREE.equals(type)) {
			// 查询系统缓存
			params.put("pattern", Constant.SYS_CACHE + "*");
		} else if (Constant.STR_FOUR.equals(type)) {
			// 查询业务缓存
			params.put("pattern", Constant.MTM_CACHE + "*");
		}
		params.put("type", type);
		IPage page  = (IPage) sysCacheService.queryPage(params);
		return RestResponse.success().put("page", page);
		
	}

	/**
	 * 删除cache
	 *
	 * @param keys
	 *            keys
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除模型", notes = "根据key删除模型（支持多key删除）")
	@SysLog("删除redis缓存")
	@RequiresPermissions("sys:cache:deleteCache")
	@PostMapping("/deleteCache")
	public RestResponse deleteBatch(String[] keys) {
		sysCacheService.deleteBatch(keys);

		return RestResponse.success();
	}
}
